$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'noc/nocresult/getMemberReceipt',
        datatype: "json",
        colModel: [
			{ label: '渠道', name: 'ly', index: 'ly', width: 80 },
			{ label: '预登记观众人数', name: 'personCount', index: 'personCount', width: 80 },
			{ label: '预登记观众家数', name: 'companyCount', index: 'personCount', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    vm.getDept();

});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		nocReceipt: {},
        startDate: "",
        endDate: "",
        templates:[],
        templateId: "",
        lys:[],
        ly:"",
        count:[],
        dept:[],
        deptId: '',
        deptName: ''
	},
    watch: {
        templateId (val,oldval){
            this.getLys();
        }
    },
    created(){
        this.getTemplates();
        //this.getLys();
    },

    mounted: function () {
        console.log(laydate)
        laydate.render({
            elem: '#startDate',
            type:'date',
            done: (value) => {
                this.date = value;
                vm.startDate = value;
            }
        }),
        laydate.render({
            elem: '#endDate',
            type:'date',
            done: (value) => {
                this.date = value;
                vm.endDate = value;
            }
        })
    },

	methods: {
		query: function () {
			vm.reload();
            vm.getMemberReceiptCount();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.nocReceipt = {};
		},
		update: function (event) {
			var tid = getSelectedRow();
			if(tid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(tid)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.nocReceipt.tid == null ? "noc/nocreceipt/save" : "noc/nocreceipt/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.nocReceipt),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var tids = getSelectedRows();
			if(tids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "noc/nocreceipt/delete",
                        contentType: "application/json",
                        data: JSON.stringify(tids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(tid){
			$.get(baseURL + "noc/nocreceipt/info/"+tid, function(r){
                vm.nocReceipt = r.nocReceipt;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			//"startDate="+vm.startDate+"&endDate="+vm.endDate
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{'start':vm.startDate,'end':vm.endDate,'tid':vm.templateId,'ly':vm.ly,'deptId':vm.deptId},
            }).trigger("reloadGrid");
		},
        getLys: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "noc/nocly/list2?limit=1000&tid="+vm.templateId+"&deptId="+vm.deptId,
                contentType: "application/json",
                data: {},
                success: function(r){
                    if(r.code == 0){
                        vm.lys = r.page.list;
                    }
                }
            });
        },
        getTemplates: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "noc/noctemplate/listByRole",
                contentType: "application/json",
                data: {},
                success: function(r){
                    if(r.code == 0){
                        vm.templates = r.list;
                        vm.getLys();
                    }
                }
            });
        },
        getMemberReceiptCount: function () {
            $.ajax({
                type: "GET",
                url: baseURL + "noc/nocresult/getMemberReceiptCount",
                contentType: "application/json",
                data: {'start':vm.startDate,'end':vm.endDate,'tid':vm.templateId,'ly':vm.ly,'deptId':vm.deptId},
                success: function(r){
                    if(r.code == 0){
                        vm.count = r.map;
                    }
                }
            });
        },
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.deptId = node[0].deptId;
                    vm.deptName = node[0].name;

                    //查询来源
                    vm.getLys();

                    layer.close(index);
                }
            });
        },
        getDept: function(){
            //加载部门树
            $.get(baseURL + "sys/dept/list", function(r){
                ztree = $.fn.zTree.init($("#deptTree"), setting, r);
                var node = ztree.getNodeByParam("deptId", vm.deptId);
                if(node != null){
                    ztree.selectNode(node);
                    vm.deptName = node.name;
                }
            })
        },
	}
});